package org.dashboard.server.dailytopten;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import org.dashboard.client.widget.dailytopten.DailyTopTenService;
import org.dashboard.shared.Props;

import java.sql.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * The server side implementation of the RPC service.
 */
@SuppressWarnings("serial")
public class DailyTopTenServiceImpl extends RemoteServiceServlet implements DailyTopTenService {

    static {
        try {
//            Class.forName("org.hsqldb.jdbcDriver").newInstance();
            Class.forName("com.ibm.db2.jcc.DB2Driver").newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public DailyTopTenServiceImpl() {
        Props props = Props.instance();
        String[] messageCountConfig = props.getTodayTopTenConfig();
    }

    public String[][] getDailyTopTen() throws IllegalArgumentException {
        String[][] result = new String[10][4];

        for (int i = 0; i != 10; i++) {
            for (int y = 0; y != 4; y++) {
                result[i][y] = "-";
            }
        }

        Connection con = null;
        try {
            Props props = Props.instance();
            String[] messageCountConfig = props.getTodayTopTenConfig();
            String sql = messageCountConfig[0];
            String url = messageCountConfig[1];
            String user = messageCountConfig[2];
            String pass = messageCountConfig[3];

            con = DriverManager.getConnection(url, user, pass);
            PreparedStatement ps = con.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            int i = 0;
            while (rs.next()) {
                result[i][0] = rs.getString(1); //timediff
                result[i][1] = rs.getString(2); //state
                String action = rs.getString(3);
                action = action.substring((action.lastIndexOf("/") + 1), action.length());
                result[i][2] = action; //action
                result[i][3] = rs.getString(4); //time of day
                i++;
            }
            ps.close();

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (con != null) con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        return result;
    }

    public String getDailyMessageFlowUrl() throws IllegalArgumentException {
        String imageUrl = "http://chart.apis.google.com/chart" +
                "   ?chxr=0,0,--MAX--|1,0,24" +
                "   &chxs=0,676767,11.5,0,lt,676767" +
                "   &chxt=y,x" +
                "   &chs=600x300" +
                "   &cht=lc" +
                "   &chco=3D7930" +
                "   &chd=t:--REPLACEME--" +
                "   &chg=10,-1,1,1" +
                "   &chls=2,4,0" +
                "   &chm=B,C5D4B5BB,0,0,0" +
                "   &chtt=Message+flow";

        String data[] = new String[24];
        for (int i = 0; i != data.length; i++) {
            data[i] = "0";
        }

        Connection con = null;
        try {
            Props props = Props.instance();
            String[] messageCountConfig = props.getTodaysMessageFlowConfig();
            String sql = messageCountConfig[0];
            String url = messageCountConfig[1];
            String user = messageCountConfig[2];
            String pass = messageCountConfig[3];

            con = DriverManager.getConnection(url, user, pass);
            PreparedStatement ps = con.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            int max = 0;
            while (rs.next()) {
                String count = rs.getString(1);
                String hour = rs.getString(2);
                int i = Integer.parseInt(count);
                max = Math.max(i, max);
                data[Integer.parseInt(hour)] = count;
            }
            ps.close();

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i != data.length; i++) {
                sb.append(data[i]);
                if ((i + 1) != data.length) {
                    sb.append(",");
                }
            }

            imageUrl = imageUrl.replaceAll("--REPLACEME--", sb.toString());
            imageUrl = imageUrl.replaceAll("--MAX--", max+"");

            System.out.println(imageUrl);

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (con != null) con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        return imageUrl;
    }

    public List<List<String>> getDailyTopTenError() throws IllegalArgumentException {
        List<List<String>> result = new ArrayList<List<String>>();

        Connection con = null;
        try {
            Props props = Props.instance();
            String[] messageCountConfig = props.getTodayTopTenErrorConfig();
            String sql = messageCountConfig[0];
            String url = messageCountConfig[1];
            String user = messageCountConfig[2];
            String pass = messageCountConfig[3];

            con = DriverManager.getConnection(url, user, pass);
            PreparedStatement ps = con.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                List<String> row = new ArrayList<String>();
                row.add(rs.getString(1)); //count
                row.add(rs.getString(2)+"..."); //error;
                result.add(row);
            }
            ps.close();

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (con != null) con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        return result;
    }
}
